namespace DotNetNuke.Services.Localization
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Modules.Admin.ResourceInstaller;
    using DotNetNuke.Services.Exceptions;
    using ICSharpCode.SharpZipLib.Zip;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Xml;
    using System.Xml.Serialization;

    public class LocaleFilePackReader
    {
        private PaLogger _ProgressLog;
        protected string EXCEPTION_FileRead;
        protected string EXCEPTION_LangPack_FileMissing;
        protected string EXCEPTION_LangPack_Install;
        protected string EXCEPTION_LangPack_ManifestLoad;
        protected string EXCEPTION_LangPack_ManifestZip;
        protected string EXCEPTION_LangPack_ResourceLoad;
        protected string LOG_LangPack_CreateLocale;
        protected string LOG_LangPack_ImportFiles;
        protected string LOG_LangPack_Job_CreateLocale;
        protected string LOG_LangPack_Job_DeserializeManifest;
        protected string LOG_LangPack_Job_ImportFiles;
        protected string LOG_LangPack_Job_LoadFiles;
        protected string LOG_LangPack_Job_LoadManifest;
        protected string LOG_LangPack_LoadFiles;
        protected PortalSettings ResourcePortalSettings = Globals.GetPortalSettings();

        public LocaleFilePackReader()
        {
            this.EXCEPTION_FileRead = this.GetLocalizedString("EXCEPTION_FileRead");
            this.EXCEPTION_LangPack_Install = this.GetLocalizedString("EXCEPTION.LangPack.Install");
            this.EXCEPTION_LangPack_ManifestZip = this.GetLocalizedString("EXCEPTION.LangPack.ManifestZip");
            this.EXCEPTION_LangPack_ManifestLoad = this.GetLocalizedString("EXCEPTION.LangPack.ManifestLoad");
            this.EXCEPTION_LangPack_FileMissing = this.GetLocalizedString("EXCEPTION.LangPack.FileMissing");
            this.EXCEPTION_LangPack_ResourceLoad = this.GetLocalizedString("EXCEPTION.LangPack.Install");
            this.LOG_LangPack_Job_LoadManifest = this.GetLocalizedString("LOG.LangPack.Job.LoadManifest");
            this.LOG_LangPack_Job_DeserializeManifest = this.GetLocalizedString("LOG.LangPack.Job.DeserializeManifest");
            this.LOG_LangPack_Job_LoadFiles = this.GetLocalizedString("LOG.LangPack.Job.LoadFiles");
            this.LOG_LangPack_Job_ImportFiles = this.GetLocalizedString("LOG.LangPack.Job.SaveFiles");
            this.LOG_LangPack_Job_CreateLocale = this.GetLocalizedString("LOG.LangPack.Job.CreateLocale");
            this.LOG_LangPack_LoadFiles = this.GetLocalizedString("LOG.LangPackReader.LoadFiles");
            this.LOG_LangPack_ImportFiles = this.GetLocalizedString("LOG.LangPackReader.UnzipFiles");
            this.LOG_LangPack_CreateLocale = this.GetLocalizedString("LOG.LangPackReader.CreateLocale");
            this._ProgressLog = new PaLogger();
        }

        protected void CreateLocale(Locale LocaleCulture)
        {
            this.ProgressLog.AddInfo(string.Format(this.LOG_LangPack_CreateLocale, LocaleCulture.Text));
            switch (new DotNetNuke.Services.Localization.Localization().AddLocale(LocaleCulture.Code, LocaleCulture.Text))
            {
                case "EXCEPTION.DuplicateLocale.Text":
                    this.ProgressLog.AddWarning(DotNetNuke.Services.Localization.Localization.GetString("Duplicate.ErrorMessage.Text"));
                    break;

                case "EXCEPTION.SaveLocale.Text":
                    this.ProgressLog.AddWarning(DotNetNuke.Services.Localization.Localization.GetString("Save.ErrorMessage.Text"));
                    break;
            }
        }

        protected string GetFullLocaleFileName(string RootPath, LocaleFileInfo LocaleFile)
        {
            string Result = RootPath + "/";
            if (LocaleFile.LocaleModule != null)
            {
                Result = Result + LocaleFile.LocaleModule + "/";
            }
            if (LocaleFile.LocalePath != null)
            {
                Result = Result + LocaleFile.LocalePath + "/";
            }
            return (Result + "App_LocalResources/" + LocaleFile.LocaleFileName);
        }

        protected LocaleFilePack GetLanguagePack(string Manifest)
        {
            XmlSerializer ManifestSerializer = new XmlSerializer(typeof(LocaleFilePack));
            LocaleFilePack LanguagePack = null;
            try
            {
                StringReader ManifestXML = new StringReader(Manifest);
                XmlTextReader XMLText = new XmlTextReader(ManifestXML);
                LanguagePack = (LocaleFilePack) ManifestSerializer.Deserialize(XMLText);
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_ManifestLoad, ex.Message));
                
            }
            return LanguagePack;
        }

        protected byte[] GetLanguagePackManifest(Stream LangPackStream)
        {
            ZipInputStream unzip = new ZipInputStream(LangPackStream);
            byte[] Buffer = new byte[0];
            try
            {
                ZipEntry entry = unzip.GetNextEntry();
                while (entry != null)
                {
                    if (((!entry.IsDirectory && (Path.GetFileName(entry.Name).ToLower() == "manifest.xml")) ? 1 : 0) != 0)
                    {
                        break;
                    }
                    entry = unzip.GetNextEntry();
                }
                int size = 0;
                if (entry == null)
                {
                    throw new Exception(this.EXCEPTION_FileRead + Conversions.ToString(Buffer.Length) + "/" + Conversions.ToString(size));
                }
                Buffer = new byte[(Convert.ToInt32(entry.Size) - 1) + 1];
                while (size < Buffer.Length)
                {
                    size += unzip.Read(Buffer, size, Buffer.Length - size);
                }
                if (size != Buffer.Length)
                {
                    throw new Exception(this.EXCEPTION_FileRead + Conversions.ToString(Buffer.Length) + "/" + Conversions.ToString(size));
                }
                return Buffer;
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_ManifestZip, ex.Message));
                
            }
            return Buffer;
        }

        private string GetLocalizedString(string key)
        {
            return DotNetNuke.Services.Localization.Localization.GetString(key, this.ResourcePortalSettings);
        }

        public PaLogger Install(Stream FileStrm)
        {
            try
            {
                FileStrm.Position = 0L;
                this.ProgressLog.StartJob(this.LOG_LangPack_Job_LoadManifest);
                string Manifest = Encoding.UTF8.GetString(this.GetLanguagePackManifest(FileStrm));
                this.ProgressLog.EndJob(this.LOG_LangPack_Job_LoadManifest);
                this.ProgressLog.StartJob(this.LOG_LangPack_Job_DeserializeManifest);
                LocaleFilePack LanguagePack = this.GetLanguagePack(Manifest);
                this.ProgressLog.EndJob(this.LOG_LangPack_Job_DeserializeManifest);
                this.ProgressLog.StartJob(this.LOG_LangPack_Job_LoadFiles);
                this.LoadLocaleFilesFromZip(LanguagePack, FileStrm);
                this.ProgressLog.EndJob(this.LOG_LangPack_Job_LoadFiles);
                this.ProgressLog.StartJob(this.LOG_LangPack_Job_ImportFiles);
                this.SaveLocaleFiles(LanguagePack);
                this.ProgressLog.EndJob(this.LOG_LangPack_Job_ImportFiles);
                this.ProgressLog.StartJob(this.LOG_LangPack_Job_CreateLocale);
                this.CreateLocale(LanguagePack.LocalePackCulture);
                this.ProgressLog.EndJob(this.LOG_LangPack_Job_CreateLocale);
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_Install, ex.Message));
                
            }
            return this.ProgressLog;
        }

        public PaLogger Install(string FileName)
        {
            FileStream strm = new FileStream(FileName, FileMode.Open);
            try
            {
                this.Install(strm);
            }
            finally
            {
                strm.Close();
            }
            return this.ProgressLog;
        }

        protected void LoadLocaleFilesFromZip(LocaleFilePack LangPack, Stream LangPackStream)
        {
            LangPackStream.Position = 0L;
            ZipInputStream unzip = new ZipInputStream(LangPackStream);
            try
            {
                for (ZipEntry entry = unzip.GetNextEntry(); entry != null; entry = unzip.GetNextEntry())
                {
                    if ((((entry.Name.ToLower() != "manifest.xml") && !entry.IsDirectory) ? 1 : 0) != 0)
                    {
                        LocaleFileInfo LocaleFile = LangPack.Files.get_LocaleFile(entry.Name);
                        if (LocaleFile != null)
                        {
                            this.ProgressLog.AddInfo(string.Format(this.LOG_LangPack_LoadFiles, LocaleFile.LocaleFileName));
                            LocaleFile.Buffer = new byte[(Convert.ToInt32(entry.Size) - 1) + 1];
                            int size = 0;
                            while (size < LocaleFile.Buffer.Length)
                            {
                                size += unzip.Read(LocaleFile.Buffer, size, LocaleFile.Buffer.Length - size);
                            }
                            if (size != LocaleFile.Buffer.Length)
                            {
                                throw new Exception(this.EXCEPTION_FileRead + Conversions.ToString(LocaleFile.Buffer.Length) + "/" + Conversions.ToString(size));
                            }
                        }
                        else
                        {
                            this.ProgressLog.AddInfo(string.Format(this.EXCEPTION_LangPack_FileMissing, entry.Name));
                        }
                    }
                }
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_ResourceLoad, ex.Message));
                
            }
        }

        protected void SaveLocaleFiles(LocaleFilePack LangPack)
        {
            IEnumerator refObjectHelperL0=null;
            string GlobalResourceDirectory = HttpContext.Current.Server.MapPath("~/App_GlobalResources");
            string ControlResourceDirectory = HttpContext.Current.Server.MapPath("~/controls/App_LocalResources");
            string ProviderDirectory = HttpContext.Current.Server.MapPath("~/providers/");
            string AdminResourceRootDirectory = HttpContext.Current.Server.MapPath("~/Admin");
            string LocalResourceRootDirectory = HttpContext.Current.Server.MapPath("~/DesktopModules");
            try
            {
                refObjectHelperL0 = LangPack.Files.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    LocaleFileInfo LocaleFile = (LocaleFileInfo) refObjectHelperL0.Current;
                    this.ProgressLog.AddInfo(string.Format(this.LOG_LangPack_ImportFiles, LocaleFile.LocaleFileName));
                    switch (LocaleFile.LocaleFileType)
                    {
                        case LocaleType.GlobalResource:
                            FileSystemUtils.SaveFile(Path.Combine(GlobalResourceDirectory, LocaleFile.LocaleFileName), LocaleFile.Buffer);
                            break;

                        case LocaleType.AdminResource:
                            FileSystemUtils.SaveFile(this.GetFullLocaleFileName(AdminResourceRootDirectory, LocaleFile), LocaleFile.Buffer);
                            break;

                        case LocaleType.ControlResource:
                            FileSystemUtils.SaveFile(Path.Combine(ControlResourceDirectory, LocaleFile.LocaleFileName), LocaleFile.Buffer);
                            break;

                        case LocaleType.LocalResource:
                            try
                            {
                                FileSystemUtils.SaveFile(this.GetFullLocaleFileName(LocalResourceRootDirectory, LocaleFile), LocaleFile.Buffer);
                            }
                            catch (Exception exception1)
                            {
                                
                                Exception ex = exception1;
                                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_ResourceLoad, ex.Message));
                                
                            }
                            break;

                        case LocaleType.ProviderResource:
                            try
                            {
                                FileSystemUtils.SaveFile(this.GetFullLocaleFileName(ProviderDirectory, LocaleFile), LocaleFile.Buffer);
                            }
                            catch (Exception exception2)
                            {
                                ProjectData.SetProjectError(exception2);
                                Exception ex = exception2;
                                this.ProgressLog.AddFailure(string.Format(this.EXCEPTION_LangPack_ResourceLoad, ex.Message));
                                
                            }
                            break;
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        public PaLogger ProgressLog
        {
            get
            {
                return this._ProgressLog;
            }
        }
    }
}

